package com.fengye.security.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import org.hibernate.Hibernate;
import org.springframework.security.core.GrantedAuthority;

import javax.persistence.*;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @author fengyexjtu@126.com
 * @date 2022年05月27日 4:31 PM
 */
@Entity
@Table(name = "mooc_permissions")
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@With
@Builder
public class Permission implements GrantedAuthority, Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;


    @Column(name = "permission_name", nullable = false, unique = true, length = 50)
    private String authority;

    // 用于前端展示的名称
    @Column(name = "display_name", nullable = false, length = 50)
    private String displayName;


    @JsonIgnore
    @ManyToMany(mappedBy = "permissions")
    @ToString.Exclude
    @Builder.Default
    private Set<Role> roles = new HashSet<>();

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
        Permission that = (Permission) o;
        return id != null && Objects.equals(id, that.id);
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
}
